package logic;

import java.util.Vector;

import main.GameDef;

public class GameRules implements Rules, GameDef{
	public int currentPlayer;
	
	public void nextPlayer(Vector <Player>players) {
		do{
			currentPlayer++;
			if(currentPlayer >= players.size()){
				currentPlayer = 0;
			}
		}while(players.get(currentPlayer).status != Player.ACTIVE);
	}
	
	public boolean isGameOver(Vector <Player>players) {
		if(players.size() < Manager.numPlayers){
			return true;
		} else {
			int cant = 0;
			updateActivePlayers(players);
			for(int i = 0; i < players.size(); i++){
				if(players.get(i).status == Player.ACTIVE){
					cant++;
				}
			}
			return (cant == 1);
		}
	}

	public void newGame(){
		currentPlayer = -1;
	}
	
	public void startGame(){
		currentPlayer = 0;
	}
	
	public int availableAttacks(Player player){
		return ((player.cantPieces(ID_CANNON) * 2) + player.cantPieces(ID_CATAPULT));	
	}

	public void updateGold(Player player){
		int gold = 100;
		
		for(int i = 0; i < player.castle.size(); i++){
			if(player.castle.get(i).status == Piece.ACTIVE){
				if(player.castle.get(i).getClass().equals(gamepieces.House.class)){
					gold += 10;
				}else if(player.castle.get(i).getClass().equals(gamepieces.Catapult.class)){
					gold--;
				}else if(player.castle.get(i).getClass().equals(gamepieces.Cannon.class)){
					gold -= 2;
				}else if(player.castle.get(i).getClass().equals(gamepieces.Interior.class)){
					gold++;
				}else if(player.castle.get(i).getClass().equals(gamepieces.Armory.class)){
					
				}
			} else {
				gold -= 10;
			}
		}
		player.gold += gold;
	}
	
	public void updateActivePlayers(Vector <Player>players){
		for(int i = 0; i < players.size(); i++){
			if(players.get(i).castle.size() > 0 && players.get(i).cantPieces() == 0){
				players.get(i).status = Player.INACTIVE;
			}
		}
	}

}
